<?php

/**
 * Load the correct database class file.
 */
function dsfm_load_db() {
	global $db;

	if ( isset( $db ) )
		return;

	require_once( INC . 'db.class.php' );

	$db = new DB(DB_NAME, DB_SERVER, DB_USER, DB_PWD);
	$db->charset(DB_CHARSET);
}

function dsfm_load_admin() {
	global $dsfm_backend, $db;

	if ( isset( $dsfm_backend ) )
		return;

	require_once( ADMIN . 'includes/backend.class.php' );

	$dsfm_backend = new Backend();

	$dsfm_directory = dir( MOD );

	while($handle = $dsfm_directory->read()){
		if($handle == "." || $handle == ".."){
		  continue;
		}

		if(is_dir($dsfm_directory->path . $handle)){
			if(file_exists( MOD . $handle ."/init.php" )){
				require_once( MOD . $handle . "/init.php" );
				$dsfm_backend->add_module($init);
				unset($init);
			}
		}
	}
}

function dsfm_load_all_module(){

	global $db;

	require_once( MOD . 'module.class.php' );

	$d = dir( MOD );

	while($handle = $d->read()){
		if($handle == "." || $handle == ".."){
			continue;
		}

		if(is_dir($d->path . $handle)){
			if(file_exists( MOD . $handle ."/". $handle.'.php' )){
				$m = "dsfm_".$handle;

				global ${$m};

				if ( isset( ${$m} ) ){
					return;
				}

				require_once( MOD . $handle ."/". $handle.'.php'  );

				eval('$dsfm_'.$handle.' = new '.ucfirst($handle).'($db);');
			}
		}
	}
}

/**
 * Sets the dsfm variables
 */
function dsfm_set_vars() {

	global $dsfm_uri;

	if(!isset( $_REQUEST['dsfm_uri'])){
		$dsfm_uri = "index.html";
	}

	foreach($_GET as $k => $v){
		$tmp = split("=", $v);
		eval("global \$".$k.";");
		eval("\$".$k."='".$v."';");
	}

	foreach($_POST as $k => $v){
		eval("global \$".$k.";");
		if(!is_array($v)){
			eval("\$".$k."='".addslashes($v)."';");
		}
	}
}

function dsfm_load_templete(){
	global $tpl;

	require_once( INC . 'rain.tpl.class.php' );

	if ( isset( $tpl ) )
		return;

    raintpl::configure( 'tpl_dir', 'html/' );
    raintpl::configure( 'cache_dir', 'tmp/' );
    raintpl::configure( 'php_enabled', true );

    if( strstr($_SERVER["REQUEST_URI"], "admin") ){
        raintpl::configure( 'base_url', URL . "admin/" );
        raintpl::configure( 'path_replace_list', array( 'img', 'link', 'script', 'input' ) );
    }else{
        raintpl::configure( 'base_url', URL );
        raintpl::configure( 'path_replace_list', array( 'a','img', 'link', 'script', 'input' ) );
    }

    $tpl = new raintpl();
}

function dsfm_browser_detection(){

	// Simple browser detection
	$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_iphone = false;

	if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false ) {
			$is_lynx = true;
		} elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'chrome') !== false ) {
			if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) {
				if ( $is_chrome = apply_filters( 'use_google_chrome_frame', is_admin() ) )
					header( 'X-UA-Compatible: chrome=1' );
				$is_winIE = ! $is_chrome;
			} else {
				$is_chrome = true;
			}
		} elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false ) {
			$is_safari = true;
		} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false ) {
			$is_gecko = true;
		} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false ) {
			$is_winIE = true;
		} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ) {
			$is_macIE = true;
		} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false ) {
			$is_opera = true;
		} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Nav') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.') !== false ) {
			$is_NS4 = true;
		}
	}

	if ( $is_safari && stripos($_SERVER['HTTP_USER_AGENT'], 'mobile') !== false )
		$is_iphone = true;

	$is_IE = ( $is_macIE || $is_winIE );

	// Server detection

	/**
	 * Whether the server software is Apache or something else
	 * @global bool $is_apache
	 */
	$is_apache = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);

	/**
	 * Whether the server software is IIS or something else
	 * @global bool $is_IIS
	 */
	$is_IIS = !$is_apache && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer') !== false);

	/**
	 * Whether the server software is IIS 7.X
	 * @global bool $is_iis7
	 */
	$is_iis7 = $is_IIS && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7.') !== false);
}
?>